package com.xoa.util.cms.directive;

import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.dao.portalTemplate.PortalTemplateMapper;
import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.portalColumn.PortalColumn;
import com.xoa.model.portalColumn.PortalColumnExample;
import com.xoa.util.common.StringUtils;
import freemarker.core.Environment;
import freemarker.template.*;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取栏目的详细信息
 */
public class CMSChannelDirective implements TemplateDirectiveModel {
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        // TODO Auto-generated method stub
        //获取标签内的栏目标识字符串
        String chnlIdentity = StringUtils.getString(map.get("id"));

        int chnlId = StringUtils.getInteger(environment.getCustomAttribute("channelId"),0);
        int siteId = StringUtils.getInteger(environment.getCustomAttribute("SITE_ID"),0);

        Writer writer = environment.getOut();
        boolean preview = (Boolean) environment.getCustomAttribute("preview");

        //根据siteIdentity查询出站点信息
        SiteMapper siteMapper = (SiteMapper) environment.getCustomAttribute("siteMapper");
        PortalColumnMapper portalColumnMapper = (PortalColumnMapper) environment.getCustomAttribute("portalColumnMapper");
        PortalTemplateMapper portalTemplateMapper = (PortalTemplateMapper) environment.getCustomAttribute("portalTemplateMapper");


        Object loopObj = environment.getCustomAttribute("loopObj");//上层循环变量
        TemplateModel this0 = environment.getVariable("this");
        Object channelId = environment.getCustomAttribute("CHANNEL_ID");

        //获取对应的栏目信息
        PortalColumn chnl = null;
        if(!"".equals(chnlIdentity)) {
            //从标识获取栏目信息
            PortalColumnExample example = new PortalColumnExample();
            PortalColumnExample.Criteria criteria = example.createCriteria();
            criteria.andPortalIdEqualTo(siteId);
            criteria.andColumnMarkEqualTo(chnlIdentity);
            List<PortalColumn> pList = portalColumnMapper.selectByExampleWithBLOBs(example);
            if(pList.size()==1){
                chnl = pList.get(0);
            }
        }else{
            //从ID获取栏目信息
            chnl = portalColumnMapper.selectByPrimaryKey(chnlId);
        }

        Map data = new HashMap();
        if(chnl==null){
            return ;
        }

        data.put("columnId",chnl.getColumnId());
        data.put("columnName",chnl.getColumnName());
        data.put("columnMark",chnl.getColumnMark());
        data.put("columnSortNo",chnl.getSortNo());

        Map _this = new HashMap();
        _this.put("instance",data);
        environment.setVariable("this",new DefaultObjectWrapper().wrap(_this));
        environment.setCustomAttribute("loopObj",data);

        if(templateDirectiveBody!=null){
            templateDirectiveBody.render(writer);
        }

        environment.setVariable("this", this0);
        environment.setCustomAttribute("loopObj", loopObj);
    }
}
